<div id="app" v-cloak>
    <el-card class="box-card">
        <el-form class="search" :inline="true" :model="vm" ref="vm" label-position="right" label-width="90px">
            <el-form-item label="角色代码" prop="code">
                <el-input v-model="vm.code" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入角色代码"></el-input>
            </el-form-item>
            <el-form-item label="角色名称" prop="name">
                <el-input v-model="vm.name" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入角色名称"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="comment">
                <el-input v-model="vm.comment" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入角色描述"></el-input>
            </el-form-item>
            <el-form-item label="开始日期" prop="startDate">
                <el-date-picker v-model="vm.startDate" clearable size="mini" placeholder="请输入开始日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
                <el-date-picker v-model="vm.endDate" clearable size="mini" placeholder="请输入结束日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="mini" icon="el-icon-search" @click="onSearch">搜索</el-button>
                <el-button type="warning" size="mini" icon="el-icon-refresh-left" @click="onReset('vm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card class="box-card">
        <el-row type="flex" justify="space-between">
            <el-button-group>
                <#if token.hasRes("/ajax/wsys/role/create")>
                <el-button type="success" size="mini" icon="el-icon-plus" @click="onCreate">新增</el-button>
                </#if>

                <#if token.hasRes("/ajax/wsys/role/delete")>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="onBitchDelete">删除</el-button>
                </#if>
            </el-button-group>

            <el-button-group>
            <#if token.hasRes("/ajax/wsys/role/template")>
                <el-tooltip effect="dark" content="Excel导入模板下载" placement="bottom">
                    <el-button size="mini" icon="el-icon-date" @click="onTemplate"></el-button>
                </el-tooltip>
            </#if>

            <#if token.hasRes("/ajax/wsys/role/imports")>
                <el-tooltip effect="dark" content="Excel导入" placement="bottom">
                    <el-button size="mini" icon="el-icon-upload2" @click="onImport"></el-button>
                </el-tooltip>
            </#if>

            <#if token.hasRes("/ajax/wsys/role/exports")>
                <el-tooltip effect="dark" content="Excel导出" placement="bottom">
                    <el-button size="mini" icon="el-icon-download" @click="onExport"></el-button>
                </el-tooltip>
            </#if>

                <el-tooltip effect="dark" content="刷新" placement="bottom">
                    <el-button size="mini" icon="el-icon-refresh" @click="onFind"></el-button>
                </el-tooltip>
            </el-button-group>
        </el-row>

        <el-table
                class="data"
                @selection-change="onSelectionChange"
                empty-text="无数据"
                :stripe="true"
                :data="result"
                size="mini">
            <el-table-column
                    align="center"
                    width="45"
                    type="selection">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="140"
                    prop="id"
                    label="主键">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="100"
                    prop="code"
                    label="角色代码">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="name"
                    label="角色名称">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="comment"
                    label="角色描述">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="140"
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="120"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <wb-table-ops :arg="scope.row">
                        <wb-table-op value="授权" icon="el-icon-edit" @click="onResEdit"></wb-table-op>
                    <#if token.hasRes("/ajax/wsys/role/update")>
                        <wb-table-op value="编辑" icon="el-icon-edit" @click="onEdit"></wb-table-op>
                    </#if>
                    <#if token.hasRes("/ajax/wsys/role/delete")>
                        <wb-table-op value="删除" icon="el-icon-delete" @click="onDelete"></wb-table-op>
                    </#if>
                    </wb-table-ops>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                background
                v-if="vm.totalCount > vm.pageSize"
                style="margin-top: 10px"
                @current-change="onPageChange"
                @size-change="onPageSizeChange"
                :current-page="vm.pageNumber"
                :page-size="vm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="vm.totalCount">
        </el-pagination>
    </el-card>

    <el-dialog
            :fullscreen="false"
            :title="form.formTitle"
            :custom-class="'dialog'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :modal-append-to-body="true"
            :visible.sync="form.formShow">

        <el-page-header slot="title" @back="form.formShow = false" :content="form.formTitle"></el-page-header>

        <el-form class=form" :model="form" :inline="true" :rules="formRules" ref="form"
                 label-position="right" label-width="90px">
            <el-form-item label="角色代码" prop="code">
                <el-input v-model="form.code" clearable size="mini" placeholder="请输入角色代码"></el-input>
            </el-form-item>
            <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" clearable size="mini" placeholder="请输入角色名称"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="comment">
                <el-input v-model="form.comment" clearable size="mini" placeholder="请输入角色描述"></el-input>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="form.formShow = false">取 消</el-button>
			<el-button size="mini" type="primary" @click="onSave">保存</el-button>
		</span>
    </el-dialog>

    <el-dialog width="540px" :title="formRes.title" :visible.sync="formRes.dialog">
        <el-row style="padding: 0 10px 10px 10px;">
            <el-col :span="12">
                <el-input
                        clearable
                        size="mini"
                        v-model="treeFilter"
                        prefix-icon="el-icon-search"
                        placeholder="输入过滤关键字"
                        @keyup.enter.native="$refs.tree.filter(treeFilter)"/>
            </el-col>
            <el-col :span="11" offset="1">
                <el-button-group>
                    <el-tooltip effect="dark" content="关键字过滤资源" placement="bottom">
                        <el-button size="mini" type="primary" icon="el-icon-search" @click="$refs.tree.filter(treeFilter)">查询</el-button>
                    </el-tooltip>
                    <el-tooltip effect="dark" content="刷新" placement="bottom">
                        <el-button size="mini" type="primary" icon="el-icon-refresh" @click="onTreeLoad">刷新</el-button>
                    </el-tooltip>
                    <el-tooltip effect="dark" content="勾选父节点时是否关联勾选所有子节点" placement="bottom">
                        <el-button v-if="!treeStrictly" @click="treeStrictly = !treeStrictly" size="mini" type="primary" icon="el-icon-share">关联</el-button>
                        <el-button v-if="treeStrictly" @click="treeStrictly = !treeStrictly" size="mini" icon="el-icon-share">关联</el-button>
                    </el-tooltip>
                </el-button-group>
            </el-col>
        </el-row>

        <el-tree
                ref="tree"
                accordion
                node-key="id"
                show-checkbox
                :data="treeData"
                @check="onCheck"
                :check-strictly="true"
                :expand-on-click-node="true"
                :filter-node-method="onTreeFilter"
                :default-expanded-keys="treeExpand">
            <div class="tree" slot-scope="{ node, data }">
                <el-tooltip class="item" effect="dark" :content="data.resValue" placement="right">
                    <span>
                        {{ data.resName }}
                        <span style="color: #409EFF">[{{data.resCode}}]</span>
                        <span style="color: #ff0000" v-if="data.free">[免费]</span>
                    </span>
                </el-tooltip>
            </div>
        </el-tree>

        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="formRes.dialog = false">关 闭</el-button>
		</span>
    </el-dialog>
</div>
<script>
    var app = new Vue({
        mixins: [mixinForMgr],
        el: "#app",
        data: {
            module: 'wsys',
            target: 'role',
            vm: {//条件及分页参数
                code: null,
                name: null,
                comment: null,
                startDate: null,
                endDate: null,
                pageNumber: 1,
                pageSize: 10,
                totalCount: 0,
                sortKey: 'CREATE_TIME',
                sortType: 'DESC'
            },
            form: {//待提交表单
                formTitle: null,
                formShow: false,
                id: null,
                code: null,
                name: null,
                comment: null,
                rowVersion: null
            },
            formRules: {
                code: [
                    {required: true, message: '角色代码不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '角色代码长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                name: [
                    {required: true, message: '角色名称不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '角色名称长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                comment: [
                    {min: 1, max: 50, message: '角色描述长度在 1 到 50 个字符', trigger: 'blur'}
                ],
            },
            formRes: {
                title: null,
                dialog: false,
                roleId: null,
                roleCode: null,
            },
            treeFilter: '',
            treeExpand: [],
            treeStrictly:true,//是否严格遵循复制不关联
            treeData: []
        },
        methods: {
            onCreate: function () {
                this.form.formTitle = "角色新增";
                this.form.formShow = true;
                this.form.id = "";
                this.form.code = "";
                this.form.name = "";
                this.form.comment = "";
            },
            onEdit: function (item) {
                this.form.formTitle = "角色编辑";
                this.form.formShow = true;
                this.form.id = item.id;
                this.form.code = item.code;
                this.form.name = item.name;
                this.form.comment = item.comment;
                this.form.rowVersion = item.rowVersion;
            },
            onDelete: function (item) {
                this.$confirm('将删除该项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    new Ajax("wsys", "role").delete({id: item.id}, function (response) {
                        if (response.errors.length > 0) {
                            nav.e(response.errors[0].message);
                        } else {
                            nav.s("删除成功");
                            this.onFind();
                        }
                    }.bind(this))
                }.bind(this)).catch(function (action) {

                });
            },
            onResEdit: function (item) {
                this.formRes.title = "分配资源";
                this.formRes.dialog = true;
                this.formRes.roleId = item.id;
                this.formRes.roleCode = item.code;
                new Ajax("wsys", "roleRes").find({
                    roleId: item.id,
                    pageSize: 0
                }, function (response) {
                    if (response.errors.length > 0) {
                        nav.e(response.errors[0].message);
                    } else {
                        this.$refs.tree.setCheckedKeys([]);
                        var ids = [];
                        for (var i = 0; i < response.result.length; i++) {
                            ids.push(response.result[i].resId);
                        }
                        this.$refs.tree.setCheckedKeys(ids);
                    }
                }.bind(this))
            },
            onCheck: function (item, status) {
                var roleId = this.formRes.roleId;
                var roleCode = this.formRes.roleCode;
                var isChecked = status.checkedKeys.indexOf(item.id) !== -1;

                var list = [];
                if(this.treeStrictly) {
                    list.push(item)
                } else {
                    [item].forTree(function (item) {
                        list.push(item);
                    })
                }

                nav.tipShow("更新中");
                list.forAsync(function (item_, next) {
                    if (isChecked === (status.checkedKeys.indexOf(item_.id) !== -1) && item_.id !== item.id) {
                        next();
                    } else {
                        new Ajax("wsys", "roleRes", "check").post({
                            roleId: roleId,
                            roleCode: roleCode,
                            resId: item_.id,
                            resCode: item_.resCode,
                            checked: isChecked,
                        }, function (response) {
                            if (response.errors.length > 0) {
                                nav.e(response.errors[0].message);
                            } else {
                                this.$refs.tree.setChecked(item_.id, isChecked);
                            }
                            next();
                        }.bind(this));
                    }
                }.bind(this), function () {
                    nav.tipClose()
                })
            },
            onTreeFilter: function (value, data) {
                if (!value) return true;
                return data.resName.indexOf(value) !== -1
                        || data.resCode.indexOf(value) !== -1
                        || data.resValue.indexOf(value) !== -1;
            },
            onTreeLoad: function () {
                new Ajax("wsys", "res", "tree").post({}, function (response) {
                    if (response.errors.length > 0) {
                        nav.e(response.errors[0].message);
                    } else {
                        this.treeData = response.result;
                        // 默认展开第一个节点
                        this.treeExpand = [response.result[0].id];
                    }
                }.bind(this))
            },
        },
        mounted: function () {
            this.onFind();
            this.onTreeLoad();
        }
    })
</script>
